package com.wocao.controller.uniapp;

import com.wocao.entity.ResponseResult;
import com.wocao.entity.ZyOwner;
import com.wocao.entity.ZyOwnerRoom;
import com.wocao.entity.ZyOwnerRoomRecord;
import com.wocao.service.ZyOwnerRoomRecordService;
import com.wocao.service.ZyOwnerRoomService;
import com.wocao.service.ZyOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 房屋绑定表  前端控制器
 * </p>
 *
 * @author gyh
 * @since 2023-12-10
 */
@CrossOrigin
@RestController
@RequestMapping("uni-app")
public class UniOwnerRoomController {

    @Autowired
    private ZyOwnerRoomService zyOwnerRoomService;
    @Autowired
    private ZyOwnerService zyOwnerService;
    @Autowired
    private ZyOwnerRoomRecordService zyOwnerRoomRecordService;

    @PostMapping("ownerRoom")
    public ResponseResult<String> addRoom(@RequestBody ZyOwnerRoom ownerRoom) {
        try {
            ownerRoom.setCreateTime(LocalDateTime.now());
            ownerRoom.setRoomStatus("0");
            ZyOwner owner = zyOwnerService.getById(ownerRoom.getOwnerId());
            ownerRoom.setCreateBy(owner.getOwnerRealName());
            zyOwnerRoomService.save(ownerRoom);

            ZyOwnerRoomRecord zyOwnerRoomRecord = new ZyOwnerRoomRecord();

            zyOwnerRoomRecord.setOwnerRoomId(ownerRoom.getOwnerRoomId());
            zyOwnerRoomRecord.setCommunityId(ownerRoom.getCommunityId());
            zyOwnerRoomRecord.setBuildingId(ownerRoom.getBuildingId());
            zyOwnerRoomRecord.setUnitId(ownerRoom.getUnitId());
            zyOwnerRoomRecord.setRoomId(ownerRoom.getRoomId());
            zyOwnerRoomRecord.setOwnerId(ownerRoom.getOwnerId());
            zyOwnerRoomRecord.setOwnerType(ownerRoom.getOwnerType());
            zyOwnerRoomRecord.setRoomStatus("0");
            zyOwnerRoomRecord.setCreateBy(owner.getOwnerRealName());
            zyOwnerRoomRecord.setCreateTime(LocalDateTime.now());
            zyOwnerRoomRecord.setRemark(ownerRoom.getRemark());

            zyOwnerRoomRecordService.save(zyOwnerRoomRecord);

            return ResponseResult.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "添加失败:" + e.getMessage());
        }
    }
}
